package org.dromara.study.controller;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import org.dromara.common.core.domain.R;
import org.dromara.common.mybatis.core.page.PageQuery;
import org.dromara.common.web.core.BaseController;
import org.dromara.study.domain.entity.Book;
import org.dromara.study.domain.vo.BookVo;
import org.dromara.study.service.BookService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/book")
public class BookController extends BaseController {

    @Autowired
    private BookService service;

    @GetMapping("/list")
    public R<List<Book>> getBookList() {
        return R.ok(service.list());
    }

    @PostMapping("/add")
    public R<Long> addBook(@RequestBody BookVo bookVo) {
        boolean save = service.save(bookVo);
        if (save) {
            return R.ok(bookVo.getId());
        } else {
            return R.fail();
        }
    }

    @GetMapping("/page")
    public R<Page<BookVo>> getPage(PageQuery pageQuery, BookVo bookVo) {
        return R.ok(service.getPage(pageQuery, bookVo));
    }

    @PostMapping("/update")
    public R<String> updateBook(@RequestBody BookVo bookVo) {
        service.updateById(bookVo);
        return R.ok();
    }

    @DeleteMapping("/delete/{id}")
    public R<String> removeBook(@PathVariable("id") Long id) {
        service.removeById(id);
        return R.ok();
    }
}
